//
//  JWSQLDateSave.m
//  aaaaaaaa
//
//  Created by paycloud110 on 16/6/3.
//  Copyright © 2016年 paycloud110. All rights reserved.
//

#import "JWSQLDateSave.h"
#import "FMDB.h"
#import "JWAlarm.h"

@interface JWSQLDateSave ()

/**  */
@property (nonatomic, strong) FMDatabaseQueue *dbQueue;
/** 数据库名 */
@property (nonatomic, copy) NSString *dateBaseName;
/** 数据库中的表名 */
@property (nonatomic, copy) NSString *dateBaseChartName;
/** 字段名 */
@property (nonatomic, copy) NSString *fieldName;
/**  */
@property (nonatomic, copy) NSString *datePath;

@end

@implementation JWSQLDateSave

/** 单例对象 */
static JWSQLDateSave *_instance = nil;
+ (instancetype)shareSQLDateSave
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[JWSQLDateSave alloc] init];
    });
    return _instance;
}
/** 创建数据库 */
- (void)createDataBase
{
    // 数据库名
    self.dateBaseName = @"t_DateBase";
    // 数据库中的表名
    self.dateBaseChartName = @"dataChart";
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite", self.dateBaseName]];
    self.datePath = path;
    
    NSString *sql = [NSString stringWithFormat:@"create table if not exists %@ (\
                     id integer primary key autoincrement,\
                     property text,\
                     propertyValue text);", self.dateBaseChartName];
    self.dbQueue = [FMDatabaseQueue databaseQueueWithPath:path];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        // 创建表
        if ([db executeUpdate:sql]) {
            NSLog(@"数据库创建成功: %@--%@", self.dateBaseName, self.dateBaseChartName);
        }
    }];
}
/** 插入一列数据 */
- (void)insertOneLineDataProperty:(NSString *)property withPropertyValue:(id)propertyValue
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:propertyValue];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"insert into dataChart (property, propertyValue) values(?, ?);", property, data];
    }];
}
/** 查询表中一条数据 */
- (void)readOneLineDataNeedProperty:(NSString *)property completedBlock:(void (^)(id propertyValue))completedBlock
{
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *resultSet = [db executeQuery:@"select * from dataChart where property = ?;", property];
        id idObject = nil;
        NSLog(@"目前表的字段数--%d", resultSet.columnCount);
        while (resultSet.next) {
            idObject = [resultSet objectForColumnName:@"propertyValue"];
        }
        id dict = [NSKeyedUnarchiver unarchiveObjectWithData:idObject];
        completedBlock(dict);
    }];
}
/** 更新表中一条数据 */
- (void)updateOneLineDataProperty:(NSString *)property withPropertyValue:(id)propertyValue
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:propertyValue];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"update dataChart set propertyValue = ? where property = ?;", data, property];
    }];
}
/** 删除表中一条数据 */
- (void)cancelOneLineDataProperty:(NSString *)property
{
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from dataChart where property = ?;", property];
    }];
}
/** 删除表中所有数据 */
- (void)cancelDataBaseAllData
{
    NSString *sql = [NSString stringWithFormat:@"delete from dataChart;"];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:sql];
    }];
}
/** 删除表 */
- (void)cancelDataBase
{
    NSString *sql = [NSString stringWithFormat:@"drop table dataChart;"];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:sql];
    }];
}
/** 获取表中所有数据 */
- (void)readDataBaseAllChartData:(void (^)(id idObject))completedBlock
{
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        NSMutableArray *array = [NSMutableArray array];
        FMResultSet *resultSet = [db executeQuery:@"select * from dataChart;"];
        while (resultSet.next) {
            NSData *data = [resultSet objectForColumnName:@"propertyValue"];
            id idObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [array addObject:idObject];
        }
        completedBlock(array);
    }];
}
/** 打开数据库 */
- (void)openDataBase
{
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        if ([db open]) {
            NSLog(@"数据库打开成功");
        }else {
            NSLog(@"数据库打开失败");
        }
    }];
}
/** 关闭数据库 */
- (void)closeDataBase
{
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        if ([db close]) {
            NSLog(@"数据库关闭成功");
        }else {
            NSLog(@"数据库关闭失败");
        }
    }];
}
#pragma mark --<layz>
- (FMDatabaseQueue *)dbQueue
{
    if (_dbQueue == nil) {
        _dbQueue = [[FMDatabaseQueue alloc] init];
    }
    return _dbQueue;
}
@end
